package ontime

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011

/**
  * @author dinghao 
  * @create 2021-07-27 18:42 
  * @message
  */
object Kafkaflink {
  def main(args: Array[String]): Unit = { //zkIP topic filterword
    val properties = new Properties()
    properties.setProperty("bootstrap.servers",args(0)+":9092")
    properties.setProperty("group.id","consumer1")
    properties.setProperty("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer")
    properties.setProperty("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer")
    properties.setProperty("auto.offset.reset", "latest")
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    val kafkaData:DataStream[String] = env.addSource(new FlinkKafkaConsumer011[String](
      args(1),new SimpleStringSchema(),properties))
    kafkaData.flatMap(_.split(" "))
      .map((_,1))
      .keyBy(_._1)
      .reduce((s1,s2)=>(s1._1,s1._2+s2._2))
      .print()
    env.execute("kafkaflink")
  }
}
